package com.zyk.file.controller;

import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.bw.zhanggm.common.AjaxResult;
import com.zyk.file.entity.File;
import com.zyk.file.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/file/")
@Scope("")
public class FileUploadController {




    @Autowired
    FileService fileService;

    @Autowired
    IFileService iFileService;


    @PostMapping("upload")
    public AjaxResult upload(Chunk chunk) throws InterruptedException {
        fileService.postFileUpload(chunk);
        if (chunk.getChunkNumber().equals(chunk.getTotalChunks())) {
            TimeUnit.SECONDS.sleep(1);
            FileResult fileResult = fileService.mergeFile(chunk);
            //保存数据，入库
            File file = new File().setFileName(fileResult.getFileName());
            iFileService.save(file);
            return AjaxResult.success(file.getId());
        }
        return AjaxResult.success();
    }


}
